package com.wh.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wh.basic.vo.AjaxResult;
import com.wh.system.annotation.PreAuthorize;
import com.wh.system.dto.EmployeeRoleDto;
import com.wh.system.pojo.EmployeeRole;
import com.wh.system.service.IEmployeeRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employeeRole")
public class EmployeeRoleController {
    @Autowired
    public IEmployeeRoleService employeeRoleService;

    /**
     * 接口：添加或修改
     * @param employeeRole 传递的实体
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "employeeRole/addOrUpdate",name = "添加或修改管理员角色")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody EmployeeRole employeeRole) {
        if ( employeeRole.getId() != null){
            employeeRoleService.upd(employeeRole);
        }else{
            employeeRoleService.add(employeeRole);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     * @param id
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "employeeRole/del",name = "根据id删除管理员角色")
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        employeeRoleService.del(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return AjaxResult 响应给前端
     */

    @PreAuthorize(sn = "employeeRole/patchDel",name = "批量删除管理员角色")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        employeeRoleService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     * @param id
     */

    @PreAuthorize(sn = "employeeRole/findOne",name = "根据id查找管理员角色")
    @GetMapping("/{id}")
    public EmployeeRole findOne(@PathVariable("id") Long id) {
        return employeeRoleService.findById(id);
    }


    /**
     * 接口：查询所有
     * @return
     */

    @PreAuthorize(sn = "employeeRole/findAll",name = "查找全部管理员角色")
    @GetMapping
    public List<EmployeeRole> findAll() {
        return employeeRoleService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     * @param employeeRoleDto 查询对象
     * @return IPage<EmployeeRole> 分页对象
     */

    @PreAuthorize(sn = "employeeRole/findByPage",name = "分页查找管理员角色")
    @PostMapping
    public IPage<EmployeeRole> findByPage(@RequestBody EmployeeRoleDto employeeRoleDto) {
        return employeeRoleService.findByPage(employeeRoleDto);
    }
}

